import React from 'react';
import jsPDF from 'jspdf/dist/jspdf.min';
import html2canvas from 'html2canvas';
class GexportPDF {
  constructor(target) {
  }

  exportPDFFunction = (map,format,resolution,titleMap,$handleCancel) => {
    var olZoom = document.getElementsByClassName('ol-zoom');
    //隐藏缩放控件
    olZoom[0].style.display = 'none';
    var dims = {
      a0: [1189, 841],
      a1: [841, 594],
      a2: [594, 420],
      a3: [420, 297],
      a4: [297, 210],
      a5: [210, 148]
    };
      document.body.style.cursor = 'progress';
      var cardImg = document.getElementById('cardImg');
      var dim = dims[format];
      var width = Math.round(dim[0] * resolution / 25.4);
      var height = Math.round(dim[1] * resolution / 25.4);
      var size = /** @type {module:ol/size~Size} */ (map.getSize());
      var extent = map.getView().calculateExtent(size);

      map.once('rendercomplete', function(event) {
        html2canvas(document.getElementsByClassName('ol-viewport')[0],{
          scale:2,
          logging:false,
          useCORS:true,
          allowTaint:true,
          backgroundColor: "transparent",
          ignoreElements:(element)=>{
            if(element.id==='printHide')
              return true;
          },
        }).then(function(canvas) {
          var data = canvas.toDataURL('image/jpeg');
          cardImg.src = data;
          //关闭弹框
          $handleCancel();
          var pdf = new jsPDF('landscape', undefined, format);
          pdf.addImage(data, 'JPEG', 0, 0, dim[0], dim[1]);
          pdf.save(titleMap+'.pdf');
          // Reset original map size
          map.setSize(size);
          map.getView().fit(extent, {size: size});
          document.body.style.cursor = 'auto';
          //显示缩放控件
          olZoom[0].style.display = 'block';
        });

      });

      // Set print size
      var printSize = [width, height];
      map.setSize(printSize);
      map.getView().fit(extent, {size: printSize});

  }

}
export default GexportPDF;
